package com.dhcc.bpm.modules.scf.util;


import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
/**
 * Create by wanghonghe on 2023/5/22.
 * description：
 */
public class MD5Utils {
    // md5相关工具方法
    protected static char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
    protected static MessageDigest messagedigest = null;

    static {
        try {
            messagedigest = MessageDigest.getInstance("MD5");
        } catch (NoSuchAlgorithmException e) {
        }
    }

    public static String getFileMD5String(File file) {
        if (!file.isFile()){
            return "";
        }
        MappedByteBuffer byteBuffer = null;
        try {
            FileInputStream in = new FileInputStream(file);
            FileChannel ch = in.getChannel();
            byteBuffer = ch.map(FileChannel.MapMode.READ_ONLY, 0,file.length());
        } catch (IOException e) {
//            Log.e(MD5Utils.class.getSimpleName(),e.getMessage());
            return "";
        }
        messagedigest.update(byteBuffer);
        return bufferToHex(messagedigest.digest());
    }

    public static String md5(String s) {
        return getMD5String(s.getBytes());
    }

    public static String getMD5String(byte[] bytes) {
        messagedigest.update(bytes);
        return bufferToHex(messagedigest.digest());
    }

    private static String bufferToHex(byte bytes[]) {
        return bufferToHex(bytes, 0, bytes.length);
    }

    private static String bufferToHex(byte bytes[], int m, int n) {
        StringBuffer stringbuffer = new StringBuffer(2 * n);
        int k = m + n;
        for (int l = m; l < k; l++) {
            appendHexPair(bytes[l], stringbuffer);
        }
        return stringbuffer.toString();
    }

    private static void appendHexPair(byte bt, StringBuffer stringbuffer) {
        char c0 = hexDigits[(bt & 0xf0) >> 4];
        char c1 = hexDigits[bt & 0xf];
        stringbuffer.append(c0);
        stringbuffer.append(c1);
    }







    public static String md5(Object obj) throws IOException, NoSuchAlgorithmException {
        // 将对象转换为字节数组
        byte[] bytes = serializeObject(obj);

        // 计算字节数组的 MD5 值
        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] md5Bytes = md.digest(bytes);

        // 将 MD5 值转换为十六进制字符串
        StringBuilder sb = new StringBuilder();
        for (byte b : md5Bytes) {
            sb.append(String.format("%02x", b));
        }
        return sb.toString();
    }

    private static byte[] serializeObject(Object obj) throws IOException {
        try (ByteArrayOutputStream bos = new ByteArrayOutputStream();
             ObjectOutputStream oos = new ObjectOutputStream(bos)) {
            oos.writeObject(obj);
            return bos.toByteArray();
        }
    }

    public static void main(String[] args) {
        System.out.println(MD5Utils.md5("22")  );

        String input = "22";
        try {
            String md5 = MD5Utils.md5(input);
            System.out.println("MD5: " + md5);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
